﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    public class SheepRepository:ISheepRepository
    {
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly AgricultureDbContext context;
        public SheepRepository(AgricultureDbContext context)
        {
            this.context = context;
        }
           

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public async Task<List<Sheepfactory>> SheepFactory()
        {
            return  context.Sheepfactories.Where(c=>c.Isdelete==0).ToList();
        }


        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="sheepFullName"></param>
        /// <param name="responsiblePerson"></param>
        /// <returns></returns>
        public async Task<List<Sheepfactory>> SheepFactoryList(string sheepFullName, string responsiblePerson)
        {

            var sheeps =  context.Sheepfactories.Where(c => c.Isdelete == 0).AsQueryable().AsNoTracking();
            if(!string.IsNullOrEmpty(sheepFullName))
            {
                sheeps = sheeps.Where(c => c.SheepFullName.Contains(sheepFullName));
            }
            if(!string.IsNullOrEmpty(responsiblePerson))
            {
                sheeps = sheeps.Where(c => c.ResponsiblePerson.Contains(responsiblePerson));

            }
            return await sheeps.ToListAsync();

        }


        /// <summary>
        /// 添加羊场
        /// </summary>
        /// <param name="sheepfactory"></param>
        /// <returns></returns>
        public async Task<int> Add(Sheepfactory sheepfactory)
        {
            context.Sheepfactories.Add(sheepfactory);
            return await context.SaveChangesAsync();
        }


        /// <summary>
        /// 删除羊场
        /// </summary>
        /// <param name="sheepId"></param>
        /// <param name="idList"></param>
        /// <returns></returns>
        public async Task<int> Delete(int sheepId, params int[] idList)
        {
            if(idList.Length<=0)
            {
                 var sheep = context.Sheepfactories.Find(sheepId);
                 sheep.Isdelete = 1;
                 return await context.SaveChangesAsync();
            }

            var sheeps = context.Sheepfactories.Where(c => idList.Contains(c.SheepId));
            foreach (var item in sheeps)
            {
                item.Isdelete = 1;                
            }

            return await context.SaveChangesAsync();

        }

        /// <summary>
        /// 修改羊场
        /// </summary>
        /// <param name="sheepfactory"></param>
        /// <returns></returns>
        public async Task<int> Update(Sheepfactory sheepfactory)
        {
            context.Sheepfactories.Update(sheepfactory);
            return await context.SaveChangesAsync();
        }


        /// <summary>
        /// 反填羊场
        /// </summary>
        /// <param name="sheepId"></param>
        /// <returns></returns>
        public async Task<Sheepfactory> GetSheep(int sheepId)
        {
            return context.Sheepfactories.Find(sheepId);
        }








    }




}
